package frog.whacker;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

    //Member Variables
	public static float mWidth;
    public static float mHeight;
    private int mObtainedScore = 0;
    private int mTotalScore = 0;
    private Bitmap mBitmap;
    ArrayList<Frogs> mFrogs = new ArrayList<Frogs>();
    public boolean mPost;
     
    private FrogManager mThread;

    private Paint mPaint = new Paint();
    
    public Panel(Context context) {
        super(context);
        getHolder().addCallback(this);
        mThread = new FrogManager(this);
        mPaint.setColor(Color.WHITE);
        mPost = false;
    }
    
    public int getTotalScore(){
    	return mTotalScore;
    }
    
    public void setTotalScore(int score){
    	mTotalScore = score;
    }
    public int getObtainedScore(){
    	return mObtainedScore;
    }
    
    public void setObtainedScore(int score){
    	mObtainedScore = score;
    }
    
    public void doDraw(ArrayList<Frogs> Frogs, Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        mBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.road);
		canvas.drawBitmap(mBitmap, 0, 0,null);
        mFrogs.clear();
        mFrogs.addAll(Frogs);     
        //mGFrogs.add(new GFrog(getResources(), Panel.mWidth/2, Panel.mHeight/2));
        synchronized (mFrogs) {
            for (Frogs element : mFrogs) {
                element.doDraw(canvas);
            }
        }

        mPost = false;
        canvas.drawText("Score: " + mObtainedScore + "/" + mTotalScore, 10, 10, mPaint);
    }
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        mWidth = width;
        mHeight = height;
    }
    
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (!mThread.isAlive()) {
            mThread = new FrogManager(this);
            mThread.setRunning(true);
            mThread.start();
        }
    }
    
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mThread.isAlive()) {
            mThread.setRunning(false);
        }
    }
//    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	if (!mPost)
    	{
	    	mPost = true;
	    	FrogDestroyer dThread;
	    	dThread = new FrogDestroyer(this,mFrogs,(int) event.getX(),(int)event.getY());
	        dThread.start();
	    	return super.onTouchEvent(event);    
    	}
    	else
    	{
    		return super.onTouchEvent(event);   
    	}

    }
    
    public void animate(long elapsedTime) {
        synchronized (mFrogs) {
            for (Frogs element : mFrogs) {
                element.animate(elapsedTime);
            }
        }
    }
}
